package com.sxt.controller;

import cn.hutool.core.util.StrUtil;
import com.sxt.common.CodeMsg;
import com.sxt.common.Constant;
import com.sxt.common.Result;
import com.sxt.common.validator.ValidatorUtil;
import com.sxt.domain.SysUser;
import com.sxt.form.SysUserForm;
import com.sxt.query.SysUserQuery;
import com.sxt.service.SysUserService;
import com.sxt.shiro.ActiveUser;
import lombok.extern.slf4j.Slf4j;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.crypto.hash.Md5Hash;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

/**
 * @description:
 * @author: Mr.xx
 * @date: 2021/3/18 20:13
 */
@Slf4j
@RestController
@RequestMapping("sysuser")
public class SysUserController {

    @Autowired
    private SysUserService sysUserService;

    /**
     * 处理登录请求
     * @param loginName
     * @param loginPassword
     * @return
     */
    @RequestMapping("login.do")
    public Object login(String loginName, String loginPassword) {

        //对密码进行加密
        Md5Hash md5Hash = new Md5Hash(loginPassword, Constant.MD5_SALT, 2);
        log.info(loginName + "-----"+ md5Hash.toString());
        //使用登录名和密码换取token
        UsernamePasswordToken usernamePasswordToken = new UsernamePasswordToken(loginName, md5Hash.toString());
        //获取认证主体
        Subject subject = SecurityUtils.getSubject();
        //进行认证
        subject.login(usernamePasswordToken);
        return new Result();
    }

    /**
     * 处理用户退出请求
     * @return
     */
    @RequestMapping("logout.do")
    public Object logout() {
        //获取认证主体
        Subject subject = SecurityUtils.getSubject();
        if (subject.isAuthenticated()) {
            subject.logout();
        }
        //跳转登录页面
        return new Result();
    }

    /**
     * 用户的分页查询
     *
     * @return
     */
    @RequestMapping("page.do")
    public Object page(SysUserQuery query) {
        return this.sysUserService.queryPage(query);
    }

    /**
     * 新增用户
     *
     * @param sysUser
     * @return
     */
    @RequestMapping("add.do")
    public Object add(SysUser sysUser) {
        // 进行数据格式校验
        ValidatorUtil.validator(sysUser);
        return sysUserService.add(sysUser);
    }

    /**
     * 重置用户密码
     *
     * @param id
     * @return
     */
    @RequestMapping("reset.do")
    public Object reset(Integer id) {
        return sysUserService.resetPassword(id);
    }

    /**
     * 修改当前用户密码
     *
     * @param password
     * @param newPassword
     * @return
     */
    @RequestMapping("updatePassword.do")
    public Object updatePassword(String password, String newPassword) {
        // 校验原密码和当前实际密码是否一致
        Subject subject = SecurityUtils.getSubject();
        ActiveUser user = (ActiveUser) subject.getPrincipal();
        SysUser sysUser = user.getSysUser();
        String loginPassword = sysUser.getLoginPassword();
        // 将输入的原密码 与当前登录用户进行密码比对
        // 将原密码加密
        password = new Md5Hash(password, Constant.MD5_SALT, 2).toString();
        if (!StrUtil.equals(loginPassword, password)) {
            return new Result(CodeMsg.USER_UPDATE_PASSWORD_ERROR);
        }
        Result rs = sysUserService.updatePassword(sysUser.getId(), newPassword);
        //密码是修改成功的
        if (rs.getCode().equals(CodeMsg.SUCCESS.code)) {
            //退出当前登录
            subject.logout();
        }
        return rs;
    }
}
